package com.yt.controller;

import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class ProviderController {
    @Value("${server.port}")
    String prot;
    //可指定问题在下面类中
    HystrixCommandProperties p;

    @GetMapping("/message/{ren}")
    @HystrixCommand(fallbackMethod = "getMessageFallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//是否熔断
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//采样的数量
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "20000"),//窗口时间
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")//错误百分比
    })
    public String getMessage(@PathVariable("ren") int ren) {
        int result = 10 / ren;
        return prot + ":正常消息 完成";
    }

    public String getMessageFallback(int ren) {
        return "fallback方法执行，出现问题" + ren;
    }

}
